<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( "../global/initialize.php");

if( !isset( $_POST ) ) {
	print( "Don't! Just... don't!" );
	exit();
}
$army_id = getPostValue( 'army_id' );
$merge_id = getPostValue( 'merge_id' );

$query = "SELECT * " .
		"FROM army " .
		"WHERE army_id = '$army_id'";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array( $result, MYSQL_ASSOC );
if( isset( $row[ 'ruler_id' ] ) && $row[ 'ruler_id' ] == $_SESSION[ 'ruler_id' ] ) {
	$name = $row[ 'name' ];
	$army_id = $row[ 'army_id' ];
	
	if( $army_id == $merge_id ) {
		$status = 'Armies can\'t merge into themselves.';
	}
	elseif( $row[ 'training' ] != 0 ) {
		$status = 'Can\'t merge an army while training.';
	}
	elseif( $row[ 'mission' ] != 0 ) {
		$status = 'Can\'t merge an army while on a mission.';
	}
	elseif( $row[ 'mission' ] != 0 ) {
		$status = 'Can\'t merge an army while moving.';
	}
	elseif( $row[ 'fortified' ] != 0 ) {
		$status = 'Can\'t merge an army while fortified.';
	}
	else {
		$query2 = "SELECT * " .
				"FROM army WHERE army_id = '$merge_id'";
		$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
		$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
		
		$query3 = "SELECT count(*) " .
				"FROM army_orders WHERE army_id = $army_id";
		$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
		$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
		
		$query4 = "SELECT count(*) " .
				"FROM army_orders WHERE army_id = '$merge_id'";
		$result4 = mysql_query( $query4 ) or die( "Query failed : " . mysql_error() );
		$row4 = mysql_fetch_array( $result4, MYSQL_ASSOC );

		if( !isset( $row2[ 'army_id' ] ) ) {
			$status = 'You can\'t merge into a non existent armies.';
		}
		elseif( $row2[ 'training' ] != 0 ) {
			$status = 'Can\'t merge into an army which is training.';
		}
		elseif( $row2[ 'mission' ] != 0 ) {
			$status = 'Can\'t merge into an army which is on a mission.';
		}
		elseif( $row2[ 'mission' ] != 0 ) {
			$status = 'Can\'t merge into an army which is moving.';
		}
		elseif( $row2[ 'fortified' ] != 0 ) {
			$status = 'Can\'t merge into an army which is fortified.';
		}
		elseif( $row[ 'x' ] != $row2[ 'x' ] || $row[ 'y' ] != $row2[ 'y' ] ) {
			$status = 'Can\'t merge into an army which is not on the same location.';
		}
		elseif( $row3[ 'count(*)' ] > 0 ) {
			$status = 'Can\'t merge into an army while still under orders.';
		}
		elseif( $row4[ 'count(*)' ] > 0 ) {
			$status = 'Can\'t merge into an army which already has orders.';
		}
		elseif( $row[ 'ruler_id' ] == $row2[ 'ruler_id' ] ) {
			$name = $row2[ 'name' ];
			$query3 = "UPDATE army " .
					"SET scout = scout + " . $row[ 'scout' ] . ", infantry = infantry + " . $row[ 'infantry' ] . ", cavalry = cavalry + " . $row[ 'cavalry' ] . ", artillery = artillery + " . $row[ 'artillery' ] . " " .
					"WHERE army_id = '$merge_id'";
			mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
			$query3 = "DELETE FROM army " .
					"WHERE army_id = '$army_id'";
			mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
			$status = 'Armies succesfully merged.';
		}
	}

	$_SESSION[ 'merge_order_status' ] = $status;
	header( "Location: army.php?name=$name" );	
}
else {
	header( "Location: armies.php" );
}
?>
